package df.bean.interfacefile;

import df.bean.db.conn.DBConnection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import df.bean.db.conn.DBConn;
import df.bean.db.table.TRN_Error;
import df.bean.obj.util.JDate;
/**
 *
 * @author i3rokei2
 */
public class ImportDcBean extends InterfaceTextFileBean {
    private ResultSet rs;
    private Statement stm;
    

	@Override
	public boolean insertData(String fn, DBConnection d) {
		// TODO Auto-generated method stub
        boolean status = true;
        ArrayList a = null;
        String[] sub_data = null;
        String temp = "";
        String message = "";
        int insert_count = 0;

        try {
            setConn(d);//connect database
            setFileName(fn);//set filename read
            copyDataFile();//copy data from file to superclass arraylist member
            a = getData();//copy data from superclass arraylist member
            stm = getConn().getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = stm.executeQuery("SELECT * FROM INT_HIS_DC");
            for(int i = 0; i<a.size(); i++){
                if(i>0 && a.get(i).toString().length()> 10){
                	this.setMessage("File Transaction DC Error Line : "+i);
                	temp = (String)a.get(i);
                	//We change "^" to "@" after import to database and split by "@" instead
                    sub_data = temp.split("[@]");
                    try{
                        rs.moveToInsertRow();
                        message = "Invoice No = "+sub_data[2]+" Invoice Date = "+sub_data[3]
                                  +" HOSPITAL_CODE = "+sub_data[0].trim();
                        this.setMessage(message);
                        rs.updateString("HOSPITAL_CODE",sub_data[0].trim());
                        rs.updateString("DOCTOR_CODE",sub_data[1].trim());
                        rs.updateString("INVOICE_NO",sub_data[2].trim());
                        rs.updateString("INVOICE_DATE",sub_data[3].trim());
                        rs.updateString("MEDICAL_FEE",sub_data[4].trim());
                        rs.updateString("ACCESSION_NUMBER", sub_data[5].trim());
                        rs.insertRow();
                        insert_count++;
                    }catch(Exception e){
                    	System.out.println(e);
                        TRN_Error.writeErrorLog(this.getConn().getConnection(), "ImportDc",  this.getMessage(), e.toString(), message,"");
                    }
                }
                
            }
            if(insert_count < a.size()-1){
                setMessage("Error : "+ ""+(a.size()- (insert_count+1))+"/"+(a.size()-1)+" records complete : "+insert_count+" records.");
            }else{
                setMessage("Complete "+(a.size()-1)+"/"+insert_count+" records.");
            }
            status = true;
            this.getConn().commitTrans();
            rs.close();
        } catch (Exception f) {
            setInfo(f.toString());
            status = false;
            this.getConn().rollBackTrans();
        }
        finally {
            try {
                    if (rs != null) { 
                    rs.close();
                    rs = null;
                }
                if (stm != null) {
                    stm.close();
                    stm = null;
                }
            }
            catch (Exception ex)  {
                System.out.println(ex);
            }
            
        }
        return status;
	}
	
	@Override
	public boolean exportData(String fn, String hp_code, String type,
			String year, String month, DBConn d, String path) {
		// TODO Auto-generated method stub
		return false;
	}

  
}
